package pannelli;
import com.cloudgarden.layout.AnchorConstraint;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import com.cloudgarden.layout.AnchorLayout;

import database.DB;

import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


import oggetti.Magazzino;
import pannelli.main.PannelloMagazzino;
import pannelli.sub.SubPannelloAccessoriInMagazzino;
import pannelli.sub.SubPannelloDatiAccessorio;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AggiungiAccessorioInMagazzino extends javax.swing.JFrame {
	private SubPannelloDatiAccessorio subPannelloDatiAccessorio1;
	private JButton Aggiungi;
	
	public AggiungiAccessorioInMagazzino() {
		super();
		initGUI();
		setVisible(true);
		centra();
		addListener();
	}
	
	private void addListener() {
		Aggiungi.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Magazzino m= subPannelloDatiAccessorio1.getAccessorioFromFields();
                                if(m!=null){
                                    if(new DB().addAccessorio(m)){
                                        dispose();
					JOptionPane.showMessageDialog(null, "Accessorio aggiunto con successo!");
					SubPannelloAccessoriInMagazzino.getInstance().initTable();
                                    }else{
					JOptionPane.showMessageDialog(null, "Si e' verificato un errore col database!");
                                    }
                                }else{
                                   JOptionPane.showMessageDialog(null, "Attenzione!\nAlmeno uno dei due campi imiei o seriale è obbligatorio!");
                                }
			}
		});
		
	}

	public void centra(){
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(new Point((dimension.width - getSize().width) / 2, 
				(dimension.height - getSize().height) / 2 ));

	}
	
	private void initGUI() {
		try {
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				subPannelloDatiAccessorio1 = new SubPannelloDatiAccessorio();
				getContentPane().add(subPannelloDatiAccessorio1, new AnchorConstraint(1, 1000, 581, 0, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				subPannelloDatiAccessorio1.setPreferredSize(new java.awt.Dimension(680, 250));
				{
					Aggiungi = new JButton();
					subPannelloDatiAccessorio1.add(Aggiungi, new AnchorConstraint(810, 591, 986, 391, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
					Aggiungi.setText("Aggiungi");
					Aggiungi.setPreferredSize(new java.awt.Dimension(136, 44));
				}
			}
			pack();
			this.setSize(696, 469);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

}
